package org.testis.wirelesssecuritymodule.core;

import org.testis.utils.TerminalApplicationExecutor;
import java.io.IOException;


/**
 *
 * @author nurhat
 */
public class WpaCliWrapper extends WirelessConnectionChecker
{

    public WpaCliWrapper(String pnpDeviceId)
    {
        super(pnpDeviceId);
    }


    public boolean isConnected()
    {
        WpaState currentStatus = getWpaState();
        if (currentStatus == WpaState.COMPLETED) {
            return true;
        /*  String ipAddress = getIpAddress();
        if (!ipAddress.startsWith("169")) {
        return true;
        }
        else {
        throw new WirelessSecurityModuleException(
        WirelessSecurityModuleExceptionType.UNEXPLAINED,
        "Connection error. Obtained ip " + ipAddress +
        " is not valid...");
        }*/
        }
        else {
            throw new WirelessSecurityModuleException(
                WirelessSecurityModuleExceptionType.UNEXPLAINED,
                "Could not connect to wireless lan. Current status is  " +
                currentStatus.name());
        }
    }
    public String getKeyManagement()
    {
        return getValueOf("key_mgmt");
    }

    public WpaState getWpaState()
    {
        String state = "";
        try {
            state = getValueOf("wpa_state");
            WpaState result = WpaState.valueOf(state);
            return result;
        } catch (IllegalArgumentException ex) {
            if (!state.equals("4WAY_HANDSHAKE")) {
                throw new WirelessSecurityModuleException(
                    WirelessSecurityModuleExceptionType.UNEXPLAINED,
                    "Current status(" +
                    state + ") is not handled....Error:" + ex.getMessage());
            }
        }
        return WpaState.FOUR_WAY_HANDSHAKE;
    }


   

    public String runCommand(String command)
    {
        String output = null;
        TerminalApplicationExecutor executor = new TerminalApplicationExecutor(
            "wpa_cli " + command + " -i " + getDeviceGuid());
        try {
            int exitCode = executor.runExec();
            if (exitCode == 0) {
                output = executor.getOutputString();
            }
            else {
                throw new WirelessSecurityModuleException(
                    WirelessSecurityModuleExceptionType.UNEXPLAINED,
                    "WpaCliWrapper:Wpa Supplicant not running or device is not connected...");
            }
        } catch (IOException ex) {
            throw new WirelessSecurityModuleException(
                WirelessSecurityModuleExceptionType.UNEXPLAINED,
                "WpaCliWrapper:Wpa Supplicant not found...Error:" + ex.
                getMessage());
        } catch (InterruptedException ex) {
            throw new WirelessSecurityModuleException(
                WirelessSecurityModuleExceptionType.UNEXPLAINED,
                "WpaCliWrapper:Interrupted...Error:" + ex.getMessage());
        }
        return output;
    }


    private String getValueOf(String key)
    {
        String obtainedOutput = runCommand("status");
        if (obtainedOutput == null) {
            return null;
        }
        String[] obtainedVals = obtainedOutput.split(System.getProperties().
                                                     getProperty(
                                                     "line.separator"));
        for (int idx = 0; idx < obtainedVals.length; idx++) {
            String[] elem = obtainedVals[idx].split("=");
            if (elem.length > 1) {
                if (elem[0].trim().equals(key)) {
                    return elem[1];
                }
            }
        }
        return null;
    }
}
